package com.example.administrator.himalaya.testPackage;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.himalaya.R;
import com.example.administrator.himalaya.entity.recommendentity.DiscoveryListEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liuchao on 2016/12/5.
 */

public class RecommendHeader2Adapter extends PagerAdapter {
    private List<View> list;
    private List<DiscoveryListEntity> listdata;
    private Context context;
    private int listdataSize;
    public RecommendHeader2Adapter(List<DiscoveryListEntity> listdata,Context context) {
        this.listdata = listdata;
        this.context=context;
        list=new ArrayList<>();
        listdataSize=listdata.size();
        intstantList();
        getPicture();


    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(list.get(position));
        return list.get(position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
//        super.destroyItem(container, position, object);
        container.removeView(list.get(position));
    }

    public void getPicture(){
        Holder holde=new Holder(list.get(0));
        Holder holde2=new Holder(list.get(1));
        for(int i=0;i<listdata.size();i++){
            if(i<4) {
                PicLoad.loadImage(holde.ivList.get(i), listdata.get(i).getCoverPath());
                holde.tvList.get(i).setText(listdata.get(i).getTitle());
            }
            else{
                PicLoad.loadImage(holde2.ivList.get(i-4), listdata.get(i).getCoverPath());
                holde2.tvList.get(i-4).setText(listdata.get(i).getTitle());

            }
        }
    }
    public void intstantList(){
       for(int i=0;i<listdataSize/3;i++) {
           View view = LayoutInflater.from(context).inflate(R.layout.recommend_item_head2, null, false);
           list.add(view);
       }
    }
   public static class Holder{
       public TextView textView01,textView02,textView03,textView04;
       private ImageView imageView01,imageView02,imageView03,imageView04;
       private List<TextView> tvList;
       private List<ImageView> ivList;
       public Holder(View view){
           textView01= (TextView) view.findViewById(R.id.tv01);
           textView02= (TextView) view.findViewById(R.id.tv02);
           textView03= (TextView) view.findViewById(R.id.tv03);
           textView04= (TextView) view.findViewById(R.id.tv04);
           imageView01= (ImageView) view.findViewById(R.id.im01);
           imageView02= (ImageView) view.findViewById(R.id.im02);
           imageView03= (ImageView) view.findViewById(R.id.im03);
           imageView04= (ImageView) view.findViewById(R.id.im04);
           tvList=new ArrayList<>();
           ivList=new ArrayList<>();
           tvList.add(textView01);
           tvList.add(textView02);
           tvList.add(textView03);
           tvList.add(textView04);
           ivList.add(imageView01);
           ivList.add(imageView02);
           ivList.add(imageView03);
           ivList.add(imageView04);
       }

   }

}
